package model.parser;

import model.semantics.Node;

import java.util.Deque;
import java.util.Queue;

public abstract class FunctionDefinition<T> {
    public String label;
    public int order;
    public int precedence;

    protected Node<T>[] getArgsFromQueue(Deque<Node<T>> queue, int num,
            boolean isPrefix) {
        int argSize;
        // Handle multi-argument functions.
        if (num < 0)
            argSize = queue.size();
        else
            argSize = num;
        Node<T>[] values = new Node[argSize];
        // FIXME
        if (isPrefix) {
            for (int i = 0; i < argSize; i++) {
                if (queue.isEmpty()) {
                    throw ParserException.BAD_ARGUMENTS;
                }

                // In prefix notation need the arguments from the back, in infix
                // from the front.
                values[i] = queue.pollFirst();
            }
        } else {
            for (int i = argSize - 1; i >= 0; i--) {
                if (queue.isEmpty()) {
                    throw ParserException.BAD_ARGUMENTS;
                }

                // In prefix notation need the arguments from the back, in infix
                // from the front.
                values[i] = queue.pollLast();
            }
        }
        return values;
    }

    public abstract Node<T> makeNewInstance(Deque<Node<T>> queue,
            boolean isPrefix);
}
